package com.sky.service.impl;/*
 * ClassName: SetMealServiceImpl
 * Package : com.sky.service.impl
 * Description:
 * @Author zKer
 * @Create 2024/4/14 22:25
 */

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public void save(SetmealDTO setmealDTO) {
        //获得套餐对象并赋值
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //插入套餐信息
        setmealMapper.insert(setmeal);

        //获取套餐菜品集合
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        setMealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public Result<SetmealVO> getById(Long setmealId) {
        //查询套餐
        Setmeal setmeal = setmealMapper.selectById(setmealId);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        //查询套餐菜品
        List<SetmealDish> setmealDishes = setMealDishMapper.selectDishBySetmealId(setmealId);
        setmealVO.setSetmealDishes(setmealDishes);
        return Result.success(setmealVO);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void delete(List<Long> ids) {
        //查看所有id的套餐起售状态
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            //若起售状态 不能删除
            if (setmeal.getStatus() == 1) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        setmealMapper.deleteBatch(ids);
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //创建新的套餐对象
        Setmeal setmeal = new Setmeal();
        //将要修改的信息复制给套餐对象
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //更新套餐信息
        setmealMapper.update(setmeal);

        //获取套餐id 删除套餐关联的菜品 重新加入关联的菜品
        Long setmealId = setmealDTO.getId();

        //删除套餐关联的菜品
        setMealDishMapper.deleteBySetmealId(setmealId);
        //重新获取套餐菜品集合
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //给套餐菜品集合中的套餐id
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        //重新加入关联的菜品
        setMealDishMapper.insertBatch(setmealDishes);


    }

    @Override
    public void startOrStop(Integer status, Long id) {


        //判断是否停售  起售则判断是否包含停售菜品
        if (status == StatusConstant.ENABLE){
            //查询套餐包含的菜品
            List<Dish> dishList = dishMapper.getdishBySetmealId(id);
            //判断菜品内容情况
            if (dishList.size() > 0 && dishList!=null){
                //遍历菜品
                for (Dish dish : dishList){
                    //判断菜品状态是否停售
                    if (dish.getStatus() == StatusConstant.DISABLE){
                        //停售则抛出异常
                        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }


        //更新套餐状态
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
